package com.rw.map.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rw.map.config.APIConfig;
import com.rw.map.model.TrailDetailIncQueryDTO;

/**
 * 
 * @author forest
 *
 */
@Component
public class RestTemplateUtils {

	private static Logger logger = LoggerFactory.getLogger(RestTemplateUtils.class);

	@Autowired
	private APIConfig apiConfig;

	@Autowired
	private RestTemplate restTemplate;

	public String httpRequestByJSON(String jsonStr, String apiUrl) {
		// 提交参数设置
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		HttpEntity<String> formEntity = new HttpEntity<>(jsonStr, headers);
		String result = restTemplate.postForObject(apiUrl, formEntity, String.class);
		logger.info("调用接口=====>{}", apiUrl);
		logger.info("请求参数======>{}", jsonStr);
		logger.info("返回的结果值======>{}", result);
		return result;
	}

	public String httpRequestByTrailDetailInc(TrailDetailIncQueryDTO dto, String apiUrl, String dateTime) {
		// 提交参数设置
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		String jsonStr = JSON.toJSONString(dto);
		JSONObject obj = JSON.parseObject(jsonStr);
		obj.put("lastTime", dateTime);
		HttpEntity<String> formEntity = new HttpEntity<>(JSON.toJSONString(obj), headers);
		String result = restTemplate.postForObject(apiUrl, formEntity, String.class);
		logger.info("调用接口=====>{}", apiUrl);
		logger.info("请求参数======>{}", jsonStr);
		logger.info("返回的结果值======>{}", result);
		return result;
	}

	public String httpRequestByForm(MultiValueMap<String, String> p, String apiUrl) {
		// 提交参数设置
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(p, headers);
		// 公共参数
		p.add("app_id", apiConfig.getAppId());
		p.add("timestamp", SignUtils.getTimeStamp());
		p.add("sign", SignUtils.genSign(p, apiConfig.getKey()));
		String result = restTemplate.postForObject(apiConfig.getUrl() + apiUrl, entity, String.class);
		logger.info("调用接口=====>{}", apiUrl);
		logger.info("返回的结果值======>{}", result);
		return result;
	}

	public String httpRequestByGet(String apiUrl) {
		ResponseEntity<String> forEntity = restTemplate.getForEntity(apiUrl, String.class);
		return forEntity.getBody();
	}
}
